﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using University.Workshop.Business;

namespace University.Workshop.Presenters.Square.Register
{
    public class PresenterRegisterNewSquare
    {
        IRegisterSquareBasicView viewRegisterBasic;
        Model.Square newSquare = new Model.Square() { SquareId=string.Empty };
        ICollection<Model.Allotment> newAllotments=new List<Model.Allotment>();
        string ubicationCroquisSquare;
        IRegisterNewSquareView view;
        public PresenterRegisterNewSquare(IRegisterNewSquareView View)
        {
            view = View;
            newSquare = new Model.Square() { SquareId = string.Empty };
            newAllotments = new List<Model.Allotment>();
            ubicationCroquisSquare = string.Empty;
        }

        public void GoStepTwo()
        {
            view.RegisterBasic.ChargeDataToBasic();
            newAllotments = view.RegisterBasic.AllotmentsToRegister;
            newSquare = view.RegisterBasic.SquareToRegister;
            ubicationCroquisSquare = view.RegisterBasic.UbicationCroquisSquareToRegister;
            if (!PassFirstStep())
                view.CallFirstPage();
            else
                view.CallSecondPage();
            ProcedureAfterCall();
        }

        public void RegisterSquare()
        {
            view.RegisterBasic.ChargeDataToBasic();
            newAllotments = view.RegisterBasic.AllotmentsToRegister;
            newSquare = view.RegisterBasic.SquareToRegister;
            ubicationCroquisSquare = view.RegisterBasic.UbicationCroquisSquareToRegister;
            if (!PassFirstStep())
            {
                view.CallFirstPage();
                ProcedureAfterCall();
            }
            else if (!PassSecondStep())
            {
                view.CallSecondPage();
                ProcedureAfterCall();
            }
            else
            {
                newSquare.Enable = true;
                ManagerSquare.NewSquare(newSquare);
                ManagerAllotment.NewAllotmentInSquare(newAllotments,newSquare);
                ManagerSquare.SaveImage(ubicationCroquisSquare,newSquare);
                view.SystemMessage("El Manzano Fue Registrado Exitosamente.!!!!");
                view.RestartRegisterSquare();
            }
        }

        private bool PassFirstStep()
        {
            try
            {
                Model.Square mp = view.RegisterBasic.SquareToRegister;
                ManagerSquare.VerifyIfSquareDataIsCorrect(mp);
                return true;
            }
            catch (Exception ex)
            {
                view.ErrorMessage(ex.Message);
                return false;
            }
        }

        private bool PassSecondStep()
        {

            try
            {
                ICollection<Model.Allotment> allots = view.RegisterBasic.AllotmentsToRegister;
                ManagerAllotment.VerifyAllotmentsIsCorrect(allots);
                return true;
            }
            catch (Exception ex)
            {
                view.ErrorMessage(ex.Message);
                return false;
            }
        }

        public void GoBackPage()
        {
            viewRegisterBasic.GoBackPage();
        }

        public void GoNextPage()
        {
            viewRegisterBasic.GoNextPage();
        }
        public void GoStepOne()
        {
            view.RegisterBasic.ChargeDataToBasic();
            newAllotments = view.RegisterBasic.AllotmentsToRegister;
            newSquare = view.RegisterBasic.SquareToRegister;
            ubicationCroquisSquare = view.RegisterBasic.UbicationCroquisSquareToRegister;
            view.CallFirstPage();
            ProcedureAfterCall();
        }

        private void ProcedureAfterCall()
        {

            //if (viewRegisterBasic != null)
            //{
            //    newAllotments = view.RegisterBasic.AllotmentsToRegister;
            //    newSquare = view.RegisterBasic.SquareToRegister;
            //    ubicationCroquisSquare = view.RegisterBasic.UbicationCroquisSquareToRegister;
            //}
            viewRegisterBasic = view.RegisterBasic;
            view.RegisterBasic.AllotmentsToRegister=newAllotments;
            view.RegisterBasic.SquareToRegister=newSquare;
            view.RegisterBasic.UbicationCroquisSquareToRegister=ubicationCroquisSquare;
            viewRegisterBasic.Update();
        }

        public void FirstTime()
        {
            view.CallFirstPage();
            ProcedureAfterCall();
        }

        public void NotifyErrorMessage(string MessageValue)
        {
            view.ErrorMessage(MessageValue);
        }
        public void NotifySystemMessage(string MessageValue)
        {
            view.SystemMessage(MessageValue);
        }
    }
}
